package com.sjz.chat4;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_main_number;
    private EditText et_main_sms;
    private Button btn_main_call;
    private Button btn_main_send;

    //长点击事件
   private  View.OnLongClickListener onLongClickListener=new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            if(v==btn_main_call) {//长按打电话
               Toast.makeText(MainActivity.this, "长按打电话", Toast.LENGTH_SHORT).show();
                //1). 创建一个Intent(隐式)
                //08-28 03:38:59.717: I/ActivityManager(1222): Displayed com.android.phone/.PrivilegedOutgoingCallBroadcaster: +570ms
                Intent intent = new Intent(Intent.ACTION_CALL);//android.intent.action.CALL
                //2). 携带数据
                String number = et_main_number.getText().toString();
                intent.setData(Uri.parse("tel:"+number));
                //3). startActivity(intent)
                startActivity(intent);

            }else if(v==btn_main_send) {//长按发短信
                Toast.makeText(MainActivity.this, "长按发短信", Toast.LENGTH_SHORT).show();

                //1). 得到SmsManager的对象
                SmsManager smsManager = SmsManager.getDefault();
                //2). 发送文本信息(短信)
                String number = et_main_number.getText().toString();
                String sms = et_main_sms.getText().toString();
                smsManager.sendTextMessage(number, null, sms, null, null);
            }
            return true;//不会再触发点击事件(表示此事件已经被消费了)

        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_main_number=findViewById(R.id.et_main_number);
        et_main_sms=findViewById(R.id.et_main_sms);
        btn_main_call=findViewById(R.id.btn_main_call);
        btn_main_send=findViewById(R.id.btn_main_send);
        btn_main_call.setOnClickListener(this);
        btn_main_send.setOnClickListener(this);

        //给视图对象设置点击监听
        btn_main_call.setOnLongClickListener(onLongClickListener);
        btn_main_send.setOnLongClickListener(onLongClickListener);


    }

    //点击事件
    @Override
    public void onClick(View v) {
          if(v==btn_main_call){//打电话 页面
              String action = "android.intent.action.DIAL";
              action = Intent.ACTION_DIAL;//一样的
              Intent intent = new Intent(action);
              //2). 携带数据
              String number = et_main_number.getText().toString();
              intent.setData(Uri.parse("tel:"+number)); //<data android:scheme="tel" />
              //3). startActivity(intent)
              startActivity(intent);
          }else if(v==btn_main_send){
               //1). 创建一个Intent(隐式)
              Intent intent = new Intent(Intent.ACTION_SENDTO);//android.intent.action.SENDTO
              //2). 携带数据(号码/内容)
              String number = et_main_number.getText().toString();
              String sms = et_main_sms.getText().toString();
              intent.setData(Uri.parse("smsto:"+number));
              //携带额外数据
              intent.putExtra("sms_body", sms);
              //3). startActivity(intent)
              startActivity(intent);
          }else{

          }
    }
}